package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.haierp.mapper.BrandMapper;
import com.haierp.model.Brand;
import com.haierp.service.IBrandService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.ShiroUtil;
import com.haierp.vo.BrandQueryVO;

@Service("brandService")
public class BrandServiceImpl extends SuperServiceImpl<BrandMapper, Brand> implements IBrandService {

	@Autowired
	private BrandMapper brandMapper;

	@Override
	public void addBrand(Brand brand) {
		String userCreate = ShiroUtil.getShiroUser().getLoginName();
		brand.setUserCreate(userCreate);
		brand.setGmtCreate(new Date());
		brand.setGmtModify(new Date());
		brandMapper.insert(brand);
	}

	@Override
	public void updateBrand(Brand brand) {
		String userModify = ShiroUtil.getShiroUser().getLoginName();
		brand.setUserModify(userModify);
		brand.setGmtModify(new Date());
		brandMapper.updateSelectiveById(brand);
	}

	@Override
	public JsonPageResult<List<Brand>> queryAllBrand() {
		JsonPageResult<List<Brand>> brandResult = new JsonPageResult<>();
		List<Brand> brandList = brandMapper.queryAllBrand();
		brandResult.setData(brandList);
		return brandResult;
	}

	@Override
	public JsonPageResult<List<Brand>> queryBrands(BrandQueryVO brandQueryVO) {
		JsonPageResult<List<Brand>> brandResult = new JsonPageResult<>();
		// 查询总条数
		Integer totalCount = brandMapper.queryBrandCount(brandQueryVO);
		// 2、查询分页记录
		if (totalCount != null && totalCount != 0L) {
			brandResult.buildPage(totalCount, brandQueryVO);
			List<Brand> queryBrands = brandMapper.queryBrands(brandQueryVO);
			brandResult.setData(queryBrands);
		} else {
			List<Brand> queryBrands = new ArrayList<>();
			brandResult.setData(queryBrands);
		}
		return brandResult;
	}

}
